package com.yylending.las.common.widgets;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.*;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import com.yyfax.infrastructure.statistics.StatService;
import com.yylending.las.R;
import com.yylending.las.lending.increase.IncreaseModel;

import java.util.HashMap;

/**
 * Created by fan on 2017/7/20.
 * Copyright  2017 www.yylending.com. All Rights Reserved.
 */
public class IncreaseLimitView extends LinearLayout implements RadioGroup.OnCheckedChangeListener {

	private static final String HAVE_INSURANCE_DETAIL = "2";
	private static final String HAVE_ACCUMULATION_FUND = "3";
	private static final String HAVE_SOCIAL_SECURITY = "4";
	private static final String INSURANCE_LESS_THAN_2Y = "2000";
	private static final String INSURANCE_GREATER_THAN_2Y = "2010";
	private static final String ACCUMULATION_FUND_LESS_THAN_1Y = "3000";
	private static final String ACCUMULATION_FUND_GREATER_THAN_1Y = "3001";
	private static final String SOCIAL_SECURITY_LESS_THAN_1Y = "4000";
	private static final String SOCIAL_SECURITY_GREATER_THAN_1Y = "4001";

	/**
	 * 保单小于两年
	 */
	@BindView(R.id.insurance_less_than_2y_rb)
	RadioButton insuranceLessThan2yRb;
	/**
	 * 保单大于两年
	 */
	@BindView(R.id.insurance_greater_than_2y_rb)
	RadioButton insuranceGreaterThan2yRb;
	/**
	 * 保单时间年限
	 */
	@BindView(R.id.policy_of_insurance_detail_two)
	TableRow policyOfInsuranceDetailTwo;
	/**
	 * 输入年缴保费
	 */
	@BindView(R.id.insurance_detail_edt)
	EditText insuranceDetailEdt;
	/**
	 * 年缴保费条目
	 */
	@BindView(R.id.policy_of_insurance_three)
	TableRow policyOfInsuranceThree;
	/**
	 * 公积金小于1年
	 */
	@BindView(R.id.less_than_1y_rb)
	RadioButton lessThan1yRb;
	/**
	 * 公积金大于1年
	 */
	@BindView(R.id.greater_than_1y_rb)
	RadioButton greaterThan1yRb;
	/**
	 * 公积金年限
	 */
	@BindView(R.id.accumulation_fund_detail_two)
	TableRow accumulationFundDetailTwo;
	/**
	 * 输入公积金缴费基数
	 */
	@BindView(R.id.accumulation_fund_edt)
	EditText accumulationFundEdt;
	/**
	 * 公积金条目
	 */
	@BindView(R.id.accumulation_fund_detail_three)
	TableRow accumulationFundDetailThree;
	/**
	 * 社保小于1年
	 */
	@BindView(R.id.social_security_less_than_1y_rb)
	RadioButton socialSecurityLessThan1yRb;
	/**
	 * 社保大于1年
	 */
	@BindView(R.id.social_security_greater_than_1y_rb)
	RadioButton socialSecurityGreaterThan1yRb;
	/**
	 * 社保缴费年限
	 */
	@BindView(R.id.social_security_detail_two)
	TableRow socialSecurityDetailTwo;
	/**
	 * 输入社保基数
	 */
	@BindView(R.id.social_security_edt)
	EditText socialSecurityEdt;
	/**
	 * 社保基数条目
	 */
	@BindView(R.id.social_security_detail_three)
	TableRow socialSecurityDetailThree;
	/**
	 * 确定按钮
	 */
	@BindView(R.id.confirm_btn)
	Button confirmBtn;

	Unbinder unbinder;
	/**
	 * 保单连续缴纳RadioGroup
	 */
	@BindView(R.id.insurance_radio_group)
	RadioGroup mInsuranceRadioGroup;
	/**
	 * 公积金连续缴纳RadioGroup
	 */
	@BindView(R.id.accumulation_fund_rg)
	RadioGroup mAccumulationFundRg;
	/**
	 * 社保连续缴纳RadioGroup
	 */
	@BindView(R.id.social_security_rg)
	RadioGroup mSocialSecurityRg;
	@BindView(R.id.insurance_detail_RG)
	RadioGroup mInsuranceDetailRG;
	@BindView(R.id.accumulation_fund_RG)
	RadioGroup mAccumulationFundRG;
	@BindView(R.id.social_security_RG)
	RadioGroup mSocialSecurityRG;


	/**
	 * 保单Item
	 */
	//IncreaseModel.PromoteInfo.PromoteItem mInsurance;
	/**
	 * 公积金Item
	 */
	//IncreaseModel.PromoteInfo.PromoteItem mAccumulationFund;
	/**
	 * 社保Item
	 */
	//IncreaseModel.PromoteInfo.PromoteItem mSocialSecurity;

	private String mOnEventId;

	public IncreaseLimitView(Context context) {
		super(context);
		init(context);
	}

	public IncreaseLimitView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public IncreaseLimitView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
	}

	private void init(Context context) {
		View view = LayoutInflater.from(context).inflate(R.layout.view_increase_limit, this, true);
		ButterKnife.bind(view);
		unbinder = ButterKnife.bind(this);
		mInsuranceDetailRG.setOnCheckedChangeListener(this);
		mAccumulationFundRG.setOnCheckedChangeListener(this);
		mSocialSecurityRG.setOnCheckedChangeListener(this);
		mAccumulationFundRg.setOnCheckedChangeListener(this);
		mSocialSecurityRg.setOnCheckedChangeListener(this);
		mInsuranceRadioGroup.setOnCheckedChangeListener(this);
		insuranceDetailEdt.addTextChangedListener(new ClassOfTextWatcher(insuranceDetailEdt));
		accumulationFundEdt.addTextChangedListener(new ClassOfTextWatcher(accumulationFundEdt));
		socialSecurityEdt.addTextChangedListener(new ClassOfTextWatcher(socialSecurityEdt));
	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		switch (checkedId) {
			case R.id.insurance_detail_have_rb:
//				mInsurance = new IncreaseModel.PromoteInfo.PromoteItem();
//				mInsurance.setType(HAVE_INSURANCE_DETAIL);
//				policyOfInsuranceDetailTwo.setVisibility(View.VISIBLE);
				break;
			case R.id.insurance_detail_not_have_rb:
				//mInsurance = null;
				insuranceDetailEdt.setText(null);
				insuranceLessThan2yRb.setChecked(false);
				insuranceGreaterThan2yRb.setChecked(false);
				policyOfInsuranceDetailTwo.setVisibility(View.GONE);
				policyOfInsuranceThree.setVisibility(View.GONE);
				break;
			case R.id.insurance_greater_than_2y_rb:
//				if (null != mInsurance) {
//					mInsurance.setTime(INSURANCE_GREATER_THAN_2Y);
//					policyOfInsuranceThree.setVisibility(View.VISIBLE);
//					insuranceGreaterThan2yRb.setChecked(true);
//				}
				break;
			case R.id.insurance_less_than_2y_rb:
//				if (null != mInsurance) {
//					mInsurance.setTime(INSURANCE_LESS_THAN_2Y);
//					policyOfInsuranceThree.setVisibility(View.VISIBLE);
//					insuranceLessThan2yRb.setChecked(true);
//				}
				break;
			case R.id.accumulation_fund_have_rb:
//				mAccumulationFund = new IncreaseModel.PromoteInfo.PromoteItem();
//				mAccumulationFund.setType(HAVE_ACCUMULATION_FUND);
//				accumulationFundDetailTwo.setVisibility(View.VISIBLE);
				break;
			case R.id.accumulation_fund_have_not_rb:
				//mAccumulationFund = null;
				accumulationFundEdt.setText(null);
				lessThan1yRb.setChecked(false);
				greaterThan1yRb.setChecked(false);
				accumulationFundDetailTwo.setVisibility(View.GONE);
				accumulationFundDetailThree.setVisibility(View.GONE);
				break;
			case R.id.less_than_1y_rb:
//				if (null != mAccumulationFund) {
//					mAccumulationFund.setTime(ACCUMULATION_FUND_LESS_THAN_1Y);
//					accumulationFundDetailThree.setVisibility(View.VISIBLE);
//					lessThan1yRb.setChecked(true);
//				}
				break;
			case R.id.greater_than_1y_rb:
//				if (null != mAccumulationFund) {
//					mAccumulationFund.setTime(ACCUMULATION_FUND_GREATER_THAN_1Y);
//					accumulationFundDetailThree.setVisibility(View.VISIBLE);
//					greaterThan1yRb.setChecked(true);
//				}
				break;
			case R.id.social_security_have_rb:
//				mSocialSecurity = new IncreaseModel.PromoteInfo.PromoteItem();
//				mSocialSecurity.setType(HAVE_SOCIAL_SECURITY);
//				socialSecurityDetailTwo.setVisibility(View.VISIBLE);
//				break;
			case R.id.social_security_not_have_rb:
				//mSocialSecurity = null;
				socialSecurityEdt.setText(null);
				socialSecurityLessThan1yRb.setChecked(false);
				socialSecurityGreaterThan1yRb.setChecked(false);
				socialSecurityDetailTwo.setVisibility(View.GONE);
				socialSecurityDetailThree.setVisibility(View.GONE);
				break;
			case R.id.social_security_less_than_1y_rb:
//				if (null != mSocialSecurity) {
//					mSocialSecurity.setTime(SOCIAL_SECURITY_LESS_THAN_1Y);
//					socialSecurityDetailThree.setVisibility(View.VISIBLE);
//					socialSecurityLessThan1yRb.setChecked(true);
//				}
				break;
			case R.id.social_security_greater_than_1y_rb:
//				if (null != mSocialSecurity) {
//					mSocialSecurity.setTime(SOCIAL_SECURITY_GREATER_THAN_1Y);
//					socialSecurityDetailThree.setVisibility(View.VISIBLE);
//					socialSecurityGreaterThan1yRb.setChecked(true);
//				}
				break;
		}
	}

	private class ClassOfTextWatcher implements TextWatcher {

		private TextView view;

		public ClassOfTextWatcher(View view) {

			if (view instanceof TextView)
				this.view = (TextView) view;
			else
				throw new ClassCastException(
						"view must be an instance Of TextView");
		}

		@Override
		public void afterTextChanged(Editable s) {
			if (!TextUtils.isEmpty(insuranceDetailEdt.getText()) || !TextUtils.isEmpty(accumulationFundEdt.getText())
					|| !TextUtils.isEmpty(socialSecurityEdt.getText())) {
				confirmBtn.setEnabled(true);
			} else {
				confirmBtn.setEnabled(false);
			}
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {

		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			if (s.toString().startsWith("0")) {
				view.setText(null);
			}
		}

	}

	/**
	 * 点击确定按钮
	 */
	@OnClick(R.id.confirm_btn)
	public void onClick() {
		//校验逻辑
//		if (null != mInsurance) {
//			if (!insuranceLessThan2yRb.isChecked() && !insuranceGreaterThan2yRb.isChecked()) {
//				mListener.illegalSubmit(R.string.please_choose_insurance_to_pay_time);
//				return;
//			}
//			if (TextUtils.isEmpty(insuranceDetailEdt.getText())) {
//				mListener.illegalSubmit(R.string.Year_pay_insurance_cost_can_not_be_empty);
//				return;
//			}
//
//		}
//		if (null != mAccumulationFund) {
//			if (!lessThan1yRb.isChecked() && !greaterThan1yRb.isChecked()) {
//				mListener.illegalSubmit(R.string.please_choose_accumulation_fund_to_pay_time);
//				return;
//			}
//			if (TextUtils.isEmpty(accumulationFundEdt.getText())) {
//				mListener.illegalSubmit(R.string.accumulation_fund_base_cannot_empty);
//				return;
//			}
//		}
//
//		if (null != mSocialSecurity) {
//			if (!socialSecurityLessThan1yRb.isChecked() && !socialSecurityGreaterThan1yRb.isChecked()) {
//				mListener.illegalSubmit(R.string.please_choose_social_security_to_pay_time);
//				return;
//			}
//			if (TextUtils.isEmpty(socialSecurityEdt.getText().toString().trim())) {
//				mListener.illegalSubmit(R.string.social_security_base_cannot_empty);
//				return;
//			}
//		}
//		IncreaseModel.PromoteInfo mPromoteInfo = new IncreaseModel.PromoteInfo();
//		if (null != mInsurance) {
//			mInsurance.setMoney(insuranceDetailEdt.getText().toString());
//			mPromoteInfo.promoteList.add(mInsurance);
//		}
//		if (null != mAccumulationFund) {
//			mAccumulationFund.setMoney(accumulationFundEdt.getText().toString());
//			mPromoteInfo.promoteList.add(mAccumulationFund);
//		}
//		if (null != mSocialSecurity) {
//			mSocialSecurity.setMoney(socialSecurityEdt.getText().toString());
//			mPromoteInfo.promoteList.add(mSocialSecurity);
//		}
//		onEvent();
		//mListener.increaseLimitSubmit(mPromoteInfo);
	}

	private IncreaseLimitListener mListener;

	public  interface IncreaseLimitListener {
		void illegalSubmit(int tipsId);

		void increaseLimitSubmit(IncreaseModel.PromoteInfo parameters);
	}

	public void setIncreaseLimitListener(IncreaseLimitListener listener) {
		mListener = listener;
	}

	public void setOnEventId(String onEventId){
		mOnEventId=onEventId;
	}

	/**
	 * 提额情况数据埋点
	 */
	private void onEvent(){
		if(TextUtils.isEmpty(mOnEventId)){
			return;
		}
		HashMap<String, String> map = new HashMap<>();
//		if (null != mInsurance) {
//			map.put(StatService.ATTRIBUTE_KEY_ONE, StatService.ATTRIBUTE_VALUE_ONE);
//			map.put(StatService.ATTRIBUTE_KEY_THREE, mInsurance.getMoney());
//			if(mInsurance.getType().equals(INSURANCE_LESS_THAN_2Y)) {
//				map.put(StatService.ATTRIBUTE_KEY_TWO, StatService.ATTRIBUTE_VALUE_ONE);
//			}else{
//				map.put(StatService.ATTRIBUTE_KEY_TWO, StatService.ATTRIBUTE_VALUE_TWO);
//			}
//		} else {
//			map.put(StatService.ATTRIBUTE_KEY_ONE, StatService.ATTRIBUTE_VALUE_TWO);
//		}
//		if (null != mAccumulationFund) {
//			map.put(StatService.ATTRIBUTE_KEY_FOUR, StatService.ATTRIBUTE_VALUE_ONE);
//			map.put(StatService.ATTRIBUTE_KEY_SIX, mAccumulationFund.getMoney());
//			if(mAccumulationFund.getType().equals(ACCUMULATION_FUND_LESS_THAN_1Y)) {
//				map.put(StatService.ATTRIBUTE_KEY_FIVE, StatService.ATTRIBUTE_VALUE_ONE);
//			}else{
//				map.put(StatService.ATTRIBUTE_KEY_FIVE, StatService.ATTRIBUTE_VALUE_TWO);
//			}
//		} else {
//			map.put(StatService.ATTRIBUTE_KEY_FOUR, StatService.ATTRIBUTE_VALUE_TWO);
//		}
//		if (null != mSocialSecurity) {
//			map.put(StatService.ATTRIBUTE_KEY_SEVEN, StatService.ATTRIBUTE_VALUE_ONE);
//			map.put(StatService.ATTRIBUTE_KEY_NINE, mSocialSecurity.getMoney());
//			if(mSocialSecurity.getType().equals(SOCIAL_SECURITY_LESS_THAN_1Y)) {
//				map.put(StatService.ATTRIBUTE_KEY_EIGHT, StatService.ATTRIBUTE_VALUE_ONE);
//			}else{
//				map.put(StatService.ATTRIBUTE_KEY_EIGHT, StatService.ATTRIBUTE_VALUE_TWO);
//			}
//		} else {
//			map.put(StatService.ATTRIBUTE_KEY_SEVEN, StatService.ATTRIBUTE_VALUE_TWO);
//		}
		StatService.onEvent(getContext(), mOnEventId, map);
	}
}
